package com.write.program.socket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.write.program.socket.message.LoginMessage;
import com.write.program.socket.message.Message;
import com.write.program.socket.message.MessageConst;

import java.io.IOException;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2019-11-20
 * Time: 23:14
 */
public class ClientSocketService extends SocketService {
    private LoginMessage loginMessage = null;

    public ClientSocketService(Socket socket) {
        super(socket);
    }

    @Override
    public Message processReceiveMessage(JSONObject messageObj) {
        int type = messageObj.getIntValue("type");
        try {
            switch (type) {
                case MessageConst.LOGIN_MESSAGE_TYPE:
                    checkLoginRet(JSON.toJavaObject(messageObj, LoginMessage.class));
                    break;

                case MessageConst.LOGOUT_MESSAGE_TYPE:
                    logout();
                    break;

                case MessageConst.CHAT_MESSAGE_TYPE:
//                    showChatMessage();
                    break;

                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void logout() {
        System.out.println("been logout");
        loginMessage = null;
        try {
            this.interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    private void checkLoginRet(LoginMessage message) throws IOException {
        System.out.println(message.getMsg());
        if (message.getCode() == LoginMessage.SUCCESS_CODE) {
            // SUCCESS

        } else if (message.getCode() == LoginMessage.FAILED_CODE) {
            // FAILED

        }
    }

}
